package com.e3.basdoc.cust.client;

import java.util.List;

import com.e3.basdoc.cust.model.CustAccountModel;
import com.e3.web.core.client.GwtAsyncCallback;
import com.e3.web.core.model.ModelHelper;
import com.e3.web.widget.client.BaseAction;
import com.e3.web.widget.client.MyButton;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ComponentPlugin;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.EditorGrid;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridSelectionModel;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.Element;

public class CustAccountGridPanel extends LayoutContainer {
	
	private CustRPCServiceAsync service = CustRPCService.Util.getInstance();
	
	protected ModelHelper<CustAccountModel> modelHelper = new ModelHelper<CustAccountModel>();
	
	private Button btnAddAccount = new MyButton(CustStateMachine.addAccount);
	private Button btnDelAccount = new MyButton(CustStateMachine.delAccount);
	
	private EditorGrid<CustAccountModel> gridPanel;
	private GridSelectionModel<CustAccountModel> gridSelectionModel;
	private ListStore<CustAccountModel> gridStore;

	private ToolBar toolbar = new ToolBar();
	
	private SelectionListener<ButtonEvent> btnSelectListener = new SelectionListener<ButtonEvent>() {
		@Override
		public void componentSelected(ButtonEvent ce) {
			MyButton item = (MyButton)ce.getSource();
			onBtnMnuEvent(item.getAction());
		}
	};
	
	@Override
	protected void onRender(Element target, int index) {
		super.onRender(target, index);
		setLayout(new FitLayout());
		setSize(400, 300);
		
		ContentPanel cp = new ContentPanel(new FitLayout());
		cp.setHeaderVisible(false);
		cp.add(getGridPanel());

		Button[] buttons = {btnAddAccount,btnDelAccount};
		initButtons(buttons);
		cp.setTopComponent(toolbar);
		
		add(cp);
	}
	
	protected void initButtons(Button[] buttons) {
		for (Button button : buttons) {
			button.addSelectionListener(btnSelectListener);
			toolbar.add(button);
		}
	}

	protected void onBtnMnuEvent(BaseAction action) {
		if(action==CustStateMachine.addAccount){
			onAdd();
		}
		else if(action == CustStateMachine.delAccount){
			onDelete();
		}
	}

	private void onDelete() {
		removeSelectedValues();
	}

	private void onAdd() {
		service.createAccountModelInstance(new GwtAsyncCallback<CustAccountModel>(this) {
			@Override
			public void doSuccess(CustAccountModel result) {
				getGridPanel().stopEditing();
				getGridStore().insert(result, 0);
				getGridPanel().startEditing(0, 0);
			}
		});
	}

	public EditorGrid<CustAccountModel> getGridPanel(){
		if(gridPanel == null){
			ColumnModel cm = new ColumnModel(getGridColumnConfigs());
			gridPanel = createGridPanel(getGridStore(),cm);
			gridPanel.setSelectionModel(getGridSelectionModel());
			afterCreateGrid(gridPanel,CustAccountModel.ACCOUNTISDEFAULT);
		}
		return gridPanel;
	}
	
	private void afterCreateGrid(Grid<CustAccountModel> grid,String... names){
		List<ColumnConfig> configs = grid.getColumnModel().getColumns();
		for (ColumnConfig columnConfig : configs) {
			for (String id : names) {
				if(columnConfig.getId().equals(id)){
					if(columnConfig instanceof ComponentPlugin){
						grid.addPlugin((ComponentPlugin)columnConfig);
					}
				}
			}
		}
	}

	private EditorGrid<CustAccountModel> createGridPanel(ListStore<CustAccountModel> store,ColumnModel cm){
		return new EditorGrid<CustAccountModel>(store,cm);
	}
	
	public ListStore<CustAccountModel> getGridStore(){
		if(gridStore == null){
			gridStore =  new ListStore<CustAccountModel>();
			gridStore.setKeyProvider(modelHelper.getModelKeyProvider());
			gridStore.setModelComparer(modelHelper.getModelComparer());
		}
		return gridStore;
	}
	
	public GridSelectionModel<CustAccountModel> getGridSelectionModel(){
		if(gridSelectionModel==null){
			gridSelectionModel = new GridSelectionModel<CustAccountModel>();
		}
		return gridSelectionModel;
	}
	
	@SuppressWarnings("unchecked")
	public <X> X getFieldValue() {
		return (X)getGridValues();
	}

	@SuppressWarnings("unchecked")
	public <X> void setFieldValue(X value) {
		List<CustAccountModel> models = (List<CustAccountModel>)value;
		setGridValues(models);
	}
	
	public List<CustAccountModel> getSelectedModels(){
		return getGridSelectionModel().getSelectedItems();
	}
	
	public List<CustAccountModel> getGridValues(){
		return getGridStore().getModels();
	}
	
	public void setGridValues(List<CustAccountModel> models){
		getGridStore().removeAll();
		if(models!=null&&models.size()>0){
			getGridStore().add(models);
		}
	}
	
	public void addGridValue(CustAccountModel model){
		getGridStore().add(model);
	}
	
	public void removeSelectedValues(){
		List<CustAccountModel> models = getSelectedModels();
		if(models!=null&&models.size()>0){
			for (CustAccountModel model : models) {
				getGridStore().remove(model);
			}
		}
		
	}

	public void clearData() {
		getGridStore().removeAll();
	}

	protected List<ColumnConfig> getGridColumnConfigs(){
		return CustHelper.INSTANCE.getCustAccountGridColumnConfigs();
	}
	
}
